<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <el-table
      border
      height="680"
      :data="dataList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        prop="areaName"
        label="区域/机构类别"
        min-width="260"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="deptName"
        label="单位"
        min-width="260"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="year"
        label="年份"
        align="center"
        width="100"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        label="单位面积综合能耗"
        align="center"
        show-overflow-tooltip
      >
        <el-table-column
          prop="areaUnitEnergy"
          label="能耗(kgce/m²)"
          align="center"
          min-width="140"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="areaUnitEnergyYearOverYear"
          label="同比(%)"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div v-if="!scope.row.areaUnitEnergyYearOverYear">0</div>
            <div v-if="scope.row.areaUnitEnergyYearOverYear > 0" class="red">
              + {{ scope.row.areaUnitEnergyYearOverYear * 100 }} %
            </div>
            <div v-if="scope.row.areaUnitEnergyYearOverYear < 0" class="green">
              - {{ scope.row.areaUnitEnergyYearOverYear * 100 }}%
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="areaUnitEnergyRangePercent"
          label="下降目标(%)"
          align="center"
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.areaUnitEnergyRangePercent * 100 }}%
          </template>
        </el-table-column>
        <el-table-column
          prop="areaUnitEnergyIsReachStandard"
          label="达标情况"
          align="center"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table-column>

      <el-table-column
        label="人均综合能耗"
        align="center"
        show-overflow-tooltip
      >
        <el-table-column
          prop="personalEnergy"
          label="能耗(kgce/p)"
          align="center"
          min-width="140"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="personalEnergyYearOverYear"
          label="同比(%)"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div v-if="!scope.row.personalEnergyYearOverYear">0</div>
            <div v-if="scope.row.personalEnergyYearOverYear > 0" class="red">
              + {{ scope.row.personalEnergyYearOverYear * 100 }} %
            </div>
            <div v-if="scope.row.personalEnergyYearOverYear < 0" class="green">
              - {{ scope.row.personalEnergyYearOverYear * 100 }}%
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="personalEnergyRangePercent"
          label="下降目标(%)"
          align="center"
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.personalEnergyRangePercent * 100 }}%
          </template>
        </el-table-column>
        <el-table-column
          prop="personalEnergyIsReachStandard"
          label="达标情况"
          align="center"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table-column>

      <el-table-column label="人均水耗" align="center" show-overflow-tooltip>
        <el-table-column
          prop="personalWater"
          label="能耗(t/p)"
          align="center"
          min-width="140"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="personalWaterYearOverYear"
          label="同比(%)"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div v-if="!scope.row.personalWaterYearOverYear">0</div>
            <div v-if="scope.row.personalWaterYearOverYear > 0" class="red">
              + {{ scope.row.personalWaterYearOverYear * 100 }} %
            </div>
            <div v-if="scope.row.personalWaterYearOverYear < 0" class="green">
              - {{ scope.row.personalWaterYearOverYear * 100 }}%
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="personalWaterRangePercent"
          label="下降目标(%)"
          align="center"
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.personalWaterRangePercent * 100 }}%
          </template>
        </el-table-column>
        <el-table-column
          prop="personalWaterIsReachStandard"
          label="达标情况"
          align="center"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table-column>

      <el-table-column label="碳排总量" align="center" show-overflow-tooltip>
        <el-table-column
          prop="personalDc"
          label="碳排(kgCO2)"
          align="center"
          min-width="140"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="personalDcYearOverYear"
          label="同比(%)"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div v-if="!scope.row.personalDcYearOverYear">0</div>
            <div v-if="scope.row.personalDcYearOverYear > 0" class="red">
              + {{ scope.row.personalDcYearOverYear * 100 }} %
            </div>
            <div v-if="scope.row.personalDcYearOverYear < 0" class="green">
              - {{ scope.row.personalDcYearOverYear * 100 }}%
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="personalDcRangePercent"
          label="下降目标(%)"
          align="center"
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.personalDcRangePercent * 100 }}%
          </template>
        </el-table-column>
        <el-table-column
          prop="personalDcIsReachStandard"
          label="达标情况"
          align="center"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[20, 40, 60, 80]"
      :page.sync="currentPage"
      :limit.sync="pageSize"
      @pagination="handlerSearchDetail"
      class="mt20"
    />
  </lb-layout>
</template>

<script>
import { list } from "@/api/energyConservation/condition.js";
import CascaderSelect from "@/components/Select/CascaderSelect";
import auth from "@/plugins/auth";
export default {
  name: "condition",
  components: {
    CascaderSelect,
  },
  dicts: [],
  data() {
    return {
      areaProps: {
        value: "id",
        children: "children",
        label: "label",
        checkStrictly: true,
        expandTrigger: "hover",
      },
      isExpandAll: true,
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            year: {
              label: "年份:",
              typeName: "el-date-picker",
              type: "year",
              size: "small",
              placeholder: "选择年份",
              "value-format": "yyyy",
            },
            areaId: {
              label: "区域:",
              render: (h) => {
                let that = this;
                let valNum = that.$refs?.form?.getFieldsValue().areaId;
                let a = (
                  <CascaderSelect
                    values={valNum}
                    value={valNum}
                    requestMethod={"get"}
                    varient={"areaDept"}
                    propOptions={that.areaProps}
                    size={"small"}
                    placeholder={"请选择区域"}
                    onInput={(value) => {
                      if (value && value.length > 0) {
                        this.$refs?.form?.setFieldValue(
                          "areaId",
                          value[value.length - 1]
                        );
                      } else {
                        this.$refs?.form?.setFieldValue("areaId", null);
                      }
                    }}
                  ></CascaderSelect>
                );
                return <div>{a}</div>;
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              display: true,
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              display: true,
              click: this.handleReset,
            },
          ],
        },
      },
      dataList: [],
      total: 0,
      currentPage: 1,
      pageSize: 20,
    };
  },
  mounted() {
    this.judgePermi();
  },
  methods: {
    judgePermi() {
      // 查询
      if (auth.hasPermi("energy:target-record:list")) {
        this.apsForm.formConfig.formSoltButton[0].display = false;
        this.apsForm.formConfig.formSoltButton[1].display = false;
        this.handlerSearchDetail();
      }
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await list(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.dataList = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .red {
  color: red;
}

::v-deep .green {
  color: rgb(56, 199, 56);
}
</style>
